// pages/bookTemplate/bookTemplate.js
import Config from "../../common/config.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tempList:{},
      tempIndex:"",
      styId:"",
      tempId:"",
      bookId:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sessionId = wx.getStorageSync('sessionId')
    var that = this

    wx.request({
      url: Config.url + '/api/book/save',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'cookie': sessionId
      },
      data:{},
      success(res) {
        that.setData({
          bookId: res.data.data.book.id
        })
      
      }
    })
    wx.request({
      url: Config.url + '/api/makeBook/selectTemplateStyle',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'cookie': sessionId
      },
      success(res) {
        console.log(res)
        that.setData({
          tempList: res.data.data.data,

        })
        var arrays = res.data.data.data
        
        console.log(that.data.tempList)
      }
    })
  },
  styList: function (e) {
    
    var that = this

    that.setData({
      tempIndex: e.currentTarget.dataset.index,
      styId: e.currentTarget.id
    })

  },
  tempList: function (e) {
    console.log(e.currentTarget.id)
    var that = this

    that.setData({
      tempId: e.currentTarget.id
    })
    var sessionId = wx.getStorageSync('sessionId')
    

    wx.request({
      url: Config.url + '/api/book/save',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'cookie': sessionId
      },
      data: { templateId: that.data.tempId, bookId: that.data.bookId},
      success(res) {
        wx.navigateTo({
          url: '../bind/bind?bookid='+that.data.bookId
        })

      }
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})